package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionEnterpriseService;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.mapper.BeSectionEnterpriseMapper;
import com.bidevalution.dao.mapper.BeSectionMethodMapper;
import com.bidevalution.support.context.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by glodon on 2019-08-30.
 */
@Service
public class BeSectionEnterpriseServiceImpl implements BeSectionEnterpriseService {
    @Autowired
    private BeSectionEnterpriseMapper beSectionEnterpriseMapper;

    @Autowired
    private BeSectionMethodMapper beSectionMethodMapper;

    /**
     * 根据标段获取投标单位列表
     * @param sectionId
     * @return
     * @throws Exception
     */
    public List<BeSectionEnterprise> getBeSectionEnterpriseList(String sectionId) throws Exception{
        return beSectionEnterpriseMapper.selectBySectionId(sectionId);
    }

    /**
     * 生成暗标号
     * @param sectionId
     * @return
     * @throws Exception
     */
    @Override
    public String generateHideNumber(String sectionId) throws Exception {
        BeSectionMethod beSectionMethod = beSectionMethodMapper.selectByPrimaryKey(sectionId);
        //查询是否是暗标
        if(beSectionMethod != null && !Constants.ZERO.equals(beSectionMethod.getIsBidHide())){
            List<BeSectionEnterprise> beSectionEnterpriseList = beSectionEnterpriseMapper.selectBySectionId(sectionId);
            if(beSectionEnterpriseList != null && beSectionEnterpriseList.size() > 0) {
                for(BeSectionEnterprise beSectionEnterprise : beSectionEnterpriseList) {
                    if(StringUtils.isBlank(beSectionEnterprise.getBidHideNumber())) {
                        String bidHideNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
                        beSectionEnterprise.setBidHideNumber(bidHideNumber);
                    }
                }
                if(beSectionEnterpriseMapper.bathUpdate(beSectionEnterpriseList)>0){
                    return "暗标号生成成功";
                }
            }
        } else {
            return "不需要生成暗标号";
        }
        return "暗标号生成失败";
    }

    /**
     * 查询需要下载并解析的投标单位信息
     * @return
     * @throws Exception
     */
    public List<BeSectionEnterprise> getImportEnterpriseList() throws Exception{
        return beSectionEnterpriseMapper.getImportEnterpriseList();
    }

    @Override
    public List<BeSectionEnterprise> queryEnterBySectionId(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.selectBySectionId(sectionId);
    }

    @Override
    public List<BeSectionEnterprise> getEnterpriseNames(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.getEnterpriseNames(sectionId);
    }
    /**
     * 根据企业的平台id，获取企业信息
     * @param ptEntGuid 企业的平台id
     * @return 返回企业信息
     */
    @Override
    public BeSectionEnterprise getEnterpriseByPtId(String ptEntGuid) {
        return beSectionEnterpriseMapper.getEnterpriseByPtId(ptEntGuid);
    }
}
